<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\card\model;

class Collection extends \app\BaseModel
{
    protected $name = "longbing_card_collection";
    protected static function init()
    {
    }
    public function bindCardList($userId, $userInfo, $page = 1, $uniacid = 0)
    {
        $page = intval($page);
        $page = $page ? $page : 1;
        $data = self::alias("a")->join("longbing_card_user_info b", "a.to_uid = b.fans_id")->join("longbing_card_job c", "b.job_id = c.id", "LEFT")->where([["a.uniacid", "=", $uniacid], ["a.uid", "=", $userId], ["a.status", "=", 1], ["b.is_staff", "=", 1], ["a.to_uid", "<>", $userId]])->field(["a.to_uid", "b.name", "b.avatar", "b.job_id", "b.phone", "b.email", "c.name as job_name", "b.name"])->order(["a.id" => "desc"])->paginate(["list_rows" => 10, "page" => $page])->toArray();
        foreach ($data["data"] as $index => $item) {
            if($item["job_name"] === NULL) {
                $data["data"][$index]["job_name"] = "未设置职位";
            }
        }
        $data["data"] = transImages($data["data"], ["avatar"]);
        return $data;
    }
    public function defaultCardList($page = 1, $uniacid = 0, $staff_id = NULL)
    {
        $page = intval($page);
        $page = $page ? $page : 1;
        $modelUserInfo = new \app\dynamic\model\UserInfo();
        $where = [["a.uniacid", "=", $uniacid], ["a.is_default", "=", 1], ["a.is_staff", "=", 1]];
        if(!empty($staff_id)) {
            $where[] = ["fans_id", "<>", $staff_id];
        }
        $data = $modelUserInfo->alias("a")->where($where)->field(["a.fans_id", "a.fans_id as to_uid", "a.name", "a.avatar", "a.job_id", "a.phone", "a.email", "b.name as job_name"])->join("longbing_card_job b", "a.job_id = b.id", "LEFT")->order(["a.top" => "desc"])->paginate(["list_rows" => 10, "page" => $page])->toArray();
        foreach ($data["data"] as $index => $item) {
            if($item["job_name"] === NULL) {
                $data["data"][$index]["job_name"] = "未设置职位";
            }
        }
        $data["data"] = transImages($data["data"], ["avatar"]);
        return $data;
    }
    public function createCollection($data)
    {
        $data["create_time"] = time();
        $result = $this->save($data);
        return !empty($result);
    }
    public function updateCollection($filter, $data)
    {
        $data["update_time"] = time();
        $result = $this->where($filter)->update($data);
        return !empty($result);
    }
    public function getCollectionCount($filter)
    {
        $result = $this->where($filter)->count();
        return $result;
    }
    public function checkCollection($filter)
    {
        $result = $this->where($filter)->field("id ,status")->find();
        if($result) {
            $result = $result->toArray();
        }
        return $result;
    }
    public function getCollection($filter)
    {
        $result = $this->where($filter)->find();
        if($result) {
            $result = $result->toArray();
        }
        return $result;
    }
    public function getCollectionJoinRateCount()
    {
        $time = time() - 86400;
        $result = $this->where([["update_rate_time", "<", $time]])->count();
        return $result;
    }
    public function listCollectionJoinRate($page_config = ["page" => 1, "page_count" => 200])
    {
        $time = time() - 86400;
        $result = $this->where([["update_rate_time", "<", $time]])->field("id")->page($page_config["page"], $page_config["page_count"])->select();
        if(!empty($result)) {
            $result = $result->toArray();
        }
        return $result;
    }
    public function getYesterdaylist($where)
    {
        $data = $this->alias("a")->join("longbing_card_user_info b", "a.to_uid = b.fans_id")->join("longbing_card_user d", "b.fans_id = d.id && d.is_staff = 1", "left")->join("longbing_card_job c", "c.id = b.job_id ", "left")->field("a.to_uid,count(a.to_uid) as number,a.uniacid,b.name,avatar,c.name as job_name,nickName,avatarUrl")->where($where)->limit(3)->group("a.to_uid")->order("number desc ,a.to_uid asc")->whereDay("a.create_time", "yesterday")->select()->toArray();
        foreach ($data as $k => $v) {
            if(!$v["name"]) {
                $data[$k]["name"] = $v["nickName"];
            }
            if(!$v["avatar"]) {
                $data[$k]["avatar"] = $v["avatarUrl"];
            }
            if(!$v["job_name"]) {
                $data[$k]["job_name"] = "未设置职位";
            }
        }
        $data = transImagesOne($data, ["avatar"]);
        return $data;
    }
    public function getlistAll($where, $page, $list_rows, $type = 0, $desc = 0)
    {
        if($desc == 0) {
            $de = "asc";
        } else {
            $de = "desc";
        }
        if($desc == 0) {
            $de1 = "desc";
        } else {
            $de1 = "asc";
        }
        $data = $this->alias("a")->join("longbing_card_user_info b", "a.to_uid = b.fans_id")->join("longbing_card_user d", "b.fans_id = d.id && d.is_staff = 1", "left")->join("longbing_card_job c", "c.id = b.job_id ", "left")->field("a.to_uid,count(a.to_uid) as number,a.uniacid,b.name,avatar,c.name as job_name,nickName,avatarUrl")->where($where)->group("a.to_uid")->order("number " . $de . ",a.to_uid " . $de1)->paginate(["list_rows" => $list_rows, "page" => $page])->each(function ($item, $key) {
            if(!$item["name"]) {
                $item["name"] = $item["nickName"];
            }
            if(!$item["avatar"]) {
                $item["avatar"] = $item["avatarUrl"];
            }
            if(!$item["job_name"]) {
                $item["job_name"] = "未设置职位";
            }
            return $item;
        })->toArray();
        $data = transImagesOne($data, ["avatar"]);
        return $data;
    }
    public function getTodaylist($where)
    {
        $data = $this->alias("a")->join("longbing_card_user c", "a.uid = c.id")->where($where)->field("a.to_uid as user_id,count(a.to_uid) as number,a.uniacid")->whereDay("a.create_time", "yesterday")->group("a.to_uid")->select()->toArray();
        if($data) {
            foreach ($data as $key => $val) {
                $data[$key]["table"] = "customer";
                $data[$key]["create_time"] = strtotime("-1 day");
            }
            $stat = new \app\dynamic\model\CardStatistics();
            $stat->createRows($data);
        }
        return $data;
    }
    public function getCount($where)
    {
        return $this->alias("a")->where($where)->count();
    }
    public function today($where)
    {
        return $this->alias("a")->leftJoin("longbing_card_count b", "a.uid = b.user_id && a.to_uid = b.to_uid")->join("longbing_card_user c", "a.uid = c.id")->where($where)->whereDay("a.create_time")->group("a.uid, a.to_uid")->count();
    }
    public function weekToday($where)
    {
        $weekday = date("Y-m-d", strtotime("-6 day"));
        $data = $this->alias("a")->leftJoin("longbing_card_count b", "a.uid = b.user_id && a.to_uid = b.to_uid")->join("longbing_card_user c", "a.uid = c.id")->where($where)->whereWeek("a.update_time", $weekday)->group("a.uid, a.to_uid")->count();
        return $data;
    }
    public function Yesterday($where)
    {
        return $this->alias("a")->leftJoin("longbing_card_count b", "a.uid = b.user_id && a.to_uid = b.to_uid")->join("longbing_card_user c", "a.uid = c.id")->where($where)->whereDay("a.create_time", "yesterday")->group("a.uid, a.to_uid")->count();
    }
    public function todayLook($where)
    {
        return $this->alias("a")->join("longbing_card_count b", "a.uid = b.user_id && a.to_uid = b.to_uid")->whereDay("a.update_time")->where($where)->count();
    }
    public function todayUid($where)
    {
        $data = $this->alias("a")->leftJoin("longbing_card_count b", "a.uid = b.user_id && a.to_uid = b.to_uid")->join("longbing_card_user c", "a.uid = c.id", "left")->where($where)->group("a.uid, a.to_uid")->count();
        return $data;
    }
    public function xian($where)
    {
        $sc1 = $this->alias("a")->field("uid,to_uid,intention")->where($where)->column("to_uid", "uid");
        return count($sc1);
    }
    public function Todayxian($where)
    {
        $sc1 = $this->alias("a")->field("uid,to_uid,intention")->whereDay("create_time")->where($where)->column("to_uid", "uid");
        return count($sc1);
    }
    public function allXian($where)
    {
        $data = $this->alias("a")->leftJoin("longbing_card_count b", "a.uid = b.user_id && a.to_uid = b.to_uid")->join("longbing_card_user c", "a.uid = c.id")->where($where)->group("a.uid, a.to_uid")->count();
        return $data;
    }
    public function getlistCount($where)
    {
        return $this->where($where)->count();
    }
    public function visit($where)
    {
        $visit_number = 0;
        $visit = $this->alias("a")->join("longbing_card_count b", "a.uid = b.user_id && a.to_uid = b.to_uid")->field("count(a.to_uid) as number")->where($where)->group("a.to_uid")->select()->toArray();
        if($visit) {
            foreach ($visit as $value) {
                $visit_number += $value["number"];
            }
        }
        return $visit_number;
    }
    public function staffInfo($where_s, $page, $list_rows)
    {
        $data = $this->alias("a")->join("longbing_card_user_info b", "a.to_uid = b.fans_id")->join("longbing_card_company c", "b.company_id = c.id")->join("longbing_card_job d", "b.job_id = d.id", "left")->join("longbing_card_user e", "e.id = b.fans_id")->where($where_s)->field("avatar,b.name,company_id,job_id,b.phone,a.create_time,c.name as company_name,d.name as job_name,b.fans_id,a.uniacid,b.create_time as start_time,a.to_uid,e.create_time as start_time")->find();
        $arr = [];
        $follow_info = [];
        $radar_count = [];
        $behavior = [];
        $ability = [];
        $Interest = [];
        $active = [];
        $start_time = 0;
        if($data) {
            $data = $data->toArray();
            if($data["job_name"] === NULL) {
                $data["job_name"] = "未设置职位";
            }
            $timediff = time() - $data["start_time"];
            $data["day"] = intval($timediff / 86400);
            $data["time"] = "今天" . date("H:i", time());
            $data["create_time"] = date("Y-m-d", $data["create_time"]);
            $collect = new Collection();
            $beginToday = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
            $endToday = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y")) - 1;
            $beginYesterday = mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
            $endYesterday = mktime(0, 0, 0, date("m"), date("d"), date("Y")) - 1;
            $beginbeforeYesterday = mktime(0, 0, 0, date("m"), date("d") - 2, date("Y"));
            $endbeforeYesterday = mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")) - 1;
            $user_id = $data["fans_id"];
            $stat = new \app\dynamic\model\CardStatistics();
            $whes[] = [["intention", "=", 0], ["a.uid", "<>", $user_id], ["a.to_uid", "=", $user_id], ["a.uniacid", "=", $data["uniacid"]]];
            $cumulative_clues = $collect->todayUid($whes);
            $todayKh = $collect->today($whes);
            $yesterdaytodayKh = $collect->Yesterday($whes);
            $whes[1] = [["a.create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $beforXs = $collect->todayUid($whes);
            $contrastKh = $yesterdaytodayKh - $beforXs;
            $whes1[] = [["a.uid", "<>", $user_id], ["a.to_uid", "=", $user_id], ["a.uniacid", "=", $data["uniacid"]]];
            $xs = $collect->todayUid($whes1);
            $whes1[1] = [["a.create_time", "BETWEEN", [$beginToday, $endToday]]];
            $todayxs = $collect->todayUid($whes1);
            $whes1[1] = [["a.create_time", "BETWEEN", [$beginYesterday, $endYesterday]]];
            $yesterdayxs = $collect->todayUid($whes1);
            $whes1[1] = [["a.create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $beforexs = $collect->todayUid($whes1);
            $contrastxs = $yesterdayxs - $beforexs;
            $whez[] = [["a.uid", "<>", $user_id], ["intention", "=", 1], ["a.to_uid", "=", $user_id], ["a.uniacid", "=", $data["uniacid"]]];
            $customer = $collect->todayUid($whez);
            $today = $collect->today($whez);
            $addTodayxs = $todayxs - $today;
            $yesterdaykh = $collect->Yesterday($whez);
            $whez[] = [["a.update_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $beforkh = $collect->todayUid($whez);
            $contrastkh = $yesterdaykh - $beforkh;
            $card_count = new CardCount();
            $wheres[] = [["to_uid", "=", $user_id], ["sign", "=", "praise"], ["type", "=", 2], ["uniacid", "=", $data["uniacid"]]];
            $visit_number = $card_count->getCount($wheres);
            $wheres[1] = [["create_time", "BETWEEN", [$beginToday, $endToday]]];
            $todayVisitNumber = $card_count->getCount($wheres);
            $wheres[1] = [["create_time", "BETWEEN", [$beginYesterday, $endYesterday]]];
            $yesterdayVisit = $card_count->getCount($wheres);
            $wheres[1] = [["create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $beforVisit = $card_count->getCount($wheres);
            $contrastVisit = $yesterdayVisit - $beforVisit;
            $whereMsg[] = [["message_type", "=", "text"], ["deleted", "=", 0], ["uniacid", "=", $this->_uniacid], ["target_id", "=", $user_id]];
            $card_message = new CardMessage();
            $message = $card_message->getCount($whereMsg);
            $whereMsg[1] = [["create_time", "BETWEEN", [$beginToday, $endToday]]];
            $today_message = $card_message->getCount($whereMsg);
            $whereMsg[1] = [["create_time", "BETWEEN", [$beginYesterday, $endYesterday]]];
            $Yesterday_message = $card_message->getCount($whereMsg);
            $whereMsg[1] = [["create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $before_message = $card_message->getCount($whereMsg);
            $zx = $card_count->zxInfo($user_id, [], 1);
            $zx = $zx + $message;
            $where1 = ["create_time", "BETWEEN", [$beginToday, $endToday]];
            $zxtoday = $card_count->zxInfo($user_id, $where1, 1);
            $zxtoday = $zxtoday + $today_message;
            $where1 = ["create_time", "BETWEEN", [$beginYesterday, $endYesterday]];
            $zxYesterday = $card_count->zxInfo($user_id, $where1, 1);
            $zxYesterday = $zxYesterday + $Yesterday_message;
            $where1 = ["create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]];
            $zxbefore = $card_count->zxInfo($user_id, $where1, 1);
            $zxbefore = $zxbefore + $before_message;
            $contrastzx = $zxYesterday - $zxbefore;
            $user_mark = new UserFollow();
            $where3[] = [["staff_id", "=", $user_id], ["status", "=", 1]];
            $mark = $user_mark->getCount($where3);
            $where3[1] = [["create_time", "BETWEEN", [$beginToday, $endToday]]];
            $today_mark = $user_mark->getCount($where3);
            $where3[1] = [["create_time", "BETWEEN", [$beginYesterday, $endYesterday]]];
            $Yesterday_mark = $user_mark->getCount($where3);
            $where3[1] = [["create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $before_mark = $user_mark->getCount($where3);
            $contrastzmark = $Yesterday_mark - $before_mark;
            $where4[] = [["to_uid", "=", $user_id], ["type", "=", 4], ["sign", "=", "praise"], ["uniacid", "=", $data["uniacid"]]];
            $forward_count = $card_count->getCount($where4);
            $today_forward_count = $card_count->gettoday($where4);
            $Yesterday_forward_count = $card_count->getYesterday($where4);
            $where4[] = [["create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $before_forward_count = $card_count->getCount($where4);
            $contrastzforward = $Yesterday_forward_count - $before_forward_count;
            $where5[] = [["to_uid", "=", $user_id], ["type", "=", 3], ["sign", "=", "praise"], ["uniacid", "=", $data["uniacid"]]];
            $dz = $card_count->getCount($where5);
            $where5[1] = [["create_time", "BETWEEN", [$beginToday, $endToday]]];
            $today_dz = $card_count->getCount($where5);
            $where5[1] = [["create_time", "BETWEEN", [$beginYesterday, $endYesterday]]];
            $Yesterday_dz = $card_count->getCount($where5);
            $where5[1] = [["create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $before_dz = $card_count->getCount($where5);
            $contrastzdz = $Yesterday_dz - $before_dz;
            $where6[] = [["to_uid", "=", $user_id], ["type", "=", 1], ["sign", "=", "copy"], ["uniacid", "=", $data["uniacid"]]];
            $bc = $card_count->getCount($where6);
            $where6[1] = [["create_time", "BETWEEN", [$beginToday, $endToday]]];
            $today_bc = $card_count->getCount($where6);
            $where6[1] = [["create_time", "BETWEEN", [$beginYesterday, $endYesterday]]];
            $Yesterday_bc = $card_count->getCount($where6);
            $where6[1] = [["create_time", "BETWEEN", [$beginbeforeYesterday, $endbeforeYesterday]]];
            $before_bc = $card_count->getCount($where6);
            $contrastzbc = $Yesterday_bc - $before_bc;
            $admin_goods = new \app\dynamic\model\CardShopOrder();
            $where7[] = [["company_id", "=", $data["company_id"]], ["to_uid", "=", $user_id], ["uniacid", "=", $data["uniacid"]]];
            $sales_count = $admin_goods->getGoosSale($where7);
            $where7[] = [["create_time", "BETWEEN", [$beginYesterday, $endYesterday]]];
            $Yesterday_sales_count = $admin_goods->getGoosSale($where7);
            $arr = ["count" => [[["name" => "累计线索(人)", "number" => $xs, "rate" => $todayKh], ["name" => "线索池(人)", "number" => $cumulative_clues, "rate" => $addTodayxs], ["name" => "累计客户(人)", "number" => $customer, "rate" => $today], ["name" => "累计访问量(次)", "number" => $visit_number, "rate" => $todayVisitNumber], ["name" => "累计咨询(次)", "number" => $zx, "rate" => $zxtoday], ["name" => "累计跟进客户(次)", "number" => $mark, "rate" => $today_mark], ["name" => "累计被转发(次)", "number" => $forward_count, "rate" => $today_forward_count], ["name" => "累计被点赞(次)", "number" => $dz, "rate" => $today_dz], ["name" => "累计被保存(次)", "number" => $bc, "rate" => $today_bc]], [["name" => "新增线索(人)", "number" => $yesterdaytodayKh, "rate" => $contrastKh], ["name" => "变动线索池(人)", "number" => $yesterdayxs, "rate" => $contrastxs], ["name" => "新增客户(人)", "number" => $yesterdaykh, "rate" => $contrastkh], ["name" => "新增访问量(次)", "number" => $yesterdayVisit, "rate" => $contrastVisit], ["name" => "新增咨询(次)", "number" => $zxYesterday, "rate" => $contrastzx], ["name" => "新增跟进客户(次)", "number" => $Yesterday_mark, "rate" => $contrastzmark], ["name" => "新增被转发(次)", "number" => $Yesterday_forward_count, "rate" => $contrastzforward], ["name" => "新增被点赞(次)", "number" => $Yesterday_dz, "rate" => $contrastzdz], ["name" => "新增被保存(次)", "number" => $Yesterday_bc, "rate" => $contrastzbc]]], "goods_sale" => [$sales_count, $Yesterday_sales_count]];
            $title1 = "日增涨";
            $title2 = "日减少";
            $title3 = "持平 -";
            foreach ($arr["count"][0] as $key => $value) {
                if(0 < $value["rate"]) {
                    $arr["count"][0][$key]["text"] = $title1;
                    $arr["count"][0][$key]["up"] = 1;
                } elseif($value["rate"] == 0) {
                    $arr["count"][0][$key]["up"] = -1;
                    $arr["count"][0][$key]["text"] = $title3;
                } else {
                    $arr["count"][0][$key]["up"] = 0;
                    $arr["count"][0][$key]["text"] = $title2;
                }
                $arr["count"][0][$key]["rate"] = abs($value["rate"]);
            }
            foreach ($arr["count"][1] as $k => $val) {
                if(0 < $val["rate"]) {
                    $arr["count"][1][$k]["text"] = $title1;
                    $arr["count"][1][$k]["up"] = 1;
                } elseif($val["rate"] == 0) {
                    $arr["count"][1][$k]["up"] = -1;
                    $arr["count"][1][$k]["text"] = $title3;
                } else {
                    $arr["count"][1][$k]["up"] = 0;
                    $arr["count"][1][$k]["text"] = $title2;
                }
                $arr["count"][1][$k]["rate"] = abs($val["rate"]);
            }
            $follow_info = UserFollow::alias("a")->join("longbing_card_user b", "a.user_id = b.id")->field(["content", "a.create_time", "user_id", "b.nickName as name"])->where([["a.staff_id", "=", $user_id]])->order("a.id", "desc")->paginate(["list_rows" => 4, "page" => 1])->toArray();
            $follow_info["data"] = lbHandelRadarDate($follow_info["data"], "create_time");
            foreach ($follow_info["data"] as $index => $item) {
                $follow_info["data"][$index]["create_time"] = date("Y-m-d H:i", $item["create_time"]);
            }
            $ardar = new CardCount();
            $radar_count = $ardar->radarList([["a.to_uid", "=", $user_id], ["a.user_id", "<>", $user_id]], 1, 4);
            $radar_count["data"] = lbHandelRadarDate($radar_count["data"], "create_time");
            if($radar_count["data"]) {
                foreach ($radar_count["data"] as $k => $v) {
                    $radar_count["data"][$k]["create_time"] = date("Y-m-d H:i", $v["create_time"]);
                }
            }
            $card_count = new CardCount();
            $behavior = $card_count->getsss($user_id, $data["uniacid"], 1);
            $card_value = new CardValue();
            $ability = $card_value->bossGetAiValue($user_id, $data["uniacid"]);
            $Interest = $ardar->doPageBossInterest(["to_uid", "=", $user_id], $data["uniacid"]);
            $active = $ardar->active($user_id, $data["uniacid"]);
            $timediff = time() - $data["start_time"];
            $start_time = intval($timediff / 86400);
        }
        $data["look"] = $arr;
        $data["interaction"] = $radar_count;
        $data["follow_info"] = $follow_info;
        $data["behavior"] = $behavior;
        $data["ability"] = $ability;
        $data["interest"] = $Interest;
        $data["active"] = $active;
        $data["day"] = $start_time;
        $data = transImagesOne($data, ["avatar"]);
        return $data;
    }
    public function getCard($uid, $uniacid)
    {
        $data = $this->alias("a")->join("longbing_card_user b", "a.to_uid = b.id AND b.is_staff = 1")->where(["uid" => $uid])->value("to_uid");
        if(empty($data)) {
            $data = \think\facade\Db::name("longbing_card_user_info")->where(["uniacid" => $uniacid, "is_default" => 1])->value("fans_id");
        }
        if(!empty($data)) {
            \think\facade\Db::name("longbing_card_user")->where(["id" => $uid])->update(["last_staff_id" => $data]);
            \think\facade\Db::name("longbing_card_user")->where(["id" => $data])->inc("auto_count");
            $key = "longbing_card_user_" . $uid;
            delCache($key, $uniacid);
        }
        return $data;
    }
}

?>